<template>
  <div>
    <el-form label-width="100px">
      <el-form-item label="分类">
        <el-input v-model="Classificationid" class="bdwidth"></el-input>
      </el-form-item>
      <el-form-item label="名称">
        <el-input v-model="brandName" class="bdwidth"></el-input>
      </el-form-item>
      <el-form-item label="输入类型">
        <el-input v-model="inputType" class="bdwidth"></el-input>
      </el-form-item>
      <el-form-item label="values">
        <el-input v-model="valuess" class="bdwidth"></el-input>
      </el-form-item>
      <el-form-item label="排序">
        <el-input v-model="reorder" class="bdwidth"></el-input>
      </el-form-item>
      <el-form-item label="新品">
        <el-switch v-model="delivery1"></el-switch>
      </el-form-item>
      <el-form-item label="显示">
        <el-switch v-model="delivery2"></el-switch>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="adddialogVisible = true"
          >确定编辑</el-button
        >
        <el-button @click="offSubmit">取消</el-button>
      </el-form-item>
    </el-form>
    <!-- 确认框 -->
    <el-dialog title="提示" :visible.sync="adddialogVisible" width="50%">
      <span>你确定编辑吗？</span>
      <span slot="footer" class="dialog-footer">
        <el-button @click="adddialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="onSubmit">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import {
  UploadAddAttributePicApi, //属性添加
} from "@/request/httpApi.js";
export default {
  data() {
    return {
      adddialogVisible: false, //确认框状态
      // 开关
      id: "", //id
      Classificationid: "", //分类
      brandName: "", //  名称
      inputType: "", //输入类型
      valuess: "",
      reorder: "", //   排序
      delivery2: false, //显示
      delivery1: false, //新品
      headers: { "X-Nideshop-Token": localStorage.getItem("token") }, //请求头
    };
  },
  methods: {
    // 确定编辑
    onSubmit() {
      // 转换默认值
      if (this.delivery1) {
        this.delivery1 = 1;
      } else {
        this.delivery1 = 0;
      }
      if (this.delivery2) {
        this.delivery2 = 1;
      } else {
        this.delivery2 = 0;
      }
      // 编辑
      UploadAddAttributePicApi({
        id: this.id, //id
        attribute_category_id: this.Classificationid, //分类id
        name: this.brandName, //名称
        input_type: this.inputType, //输入类型
        values: this.valuess,
        sort_order: this.reorder, //排序
        is_show: this.delivery2, //显示
        is_new: this.delivery1, //新品
      }).then((res) => {
        if (res.errno === 0) {
          this.$router.push("/Attribute");
          this.adddialogVisible = false;
        }
      });
    },
    // 取消编辑
    offSubmit() {
      this.$router.push("/Attribute");
    },
  },
  created() {
    this.id = this.$route.query.id;
    UploadAddAttributePicApi({
      id: this.id, //id
    }).then((res) => {
      if (res.errno === 0) {
        this.brandid = res.data.id; //id
        this.Classificationid = res.data.attribute_category_id; //分类id
        this.brandName = res.data.name; //名称
        this.inputType = res.data.input_type; //输入类型
        this.valuess = res.data.values;
        this.reorder = res.data.sort_order; //排序
        this.delivery2 = res.data.is_show; //显示
        this.delivery1 = res.data.is_new; //新品
      }
    });
    // 初始化状态
    this.adddialogVisible = false;
  },
};
</script>
 
<style lang = "less" scoped>
.pictureLocation {
  width: 187px;
  height: 105px;
  line-height: 105px;
  border: 1px solid #ccc;
}
.bdwidth {
  width: 250px;
}
</style>